package com.huawei.push.controller;

import com.alibaba.fastjson.JSONObject;
import com.huawei.push.controller.Json.ResultJson;
import com.huawei.push.controller.enity.MsgEnity;
import com.huawei.push.controller.service.HuaWeiAppSendMsg;
import com.huawei.push.controller.service.MeiZuAppSendMsg;
import com.huawei.push.controller.service.VivoAppSendMsg;
import com.huawei.push.controller.vo.HuaWeiRequestVo;
import com.huawei.push.controller.vo.MeiZuRequestVo;
import com.huawei.push.controller.vo.VivoRequestVo;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class TestController {

    @Resource
    HuaWeiAppSendMsg huaWeiAppSendMsg;

    @Resource
    VivoAppSendMsg vivoAppSendMsg;

    @Resource
    MeiZuAppSendMsg meiZuAppSendMsg;

    /**
     * 华为的推送消息
     * @return
     */
    @RequestMapping("/huawei")
    public String sendHuaWei()  {
        List<String> tokens= new ArrayList<>();
        tokens.add("7780918728129181");
        tokens.add("2189787900978900");

//        设置消息信息
        HuaWeiRequestVo huaWeiRequestVo = new HuaWeiRequestVo();

        huaWeiRequestVo.setContent("消息体");
        huaWeiRequestVo.setTitle("消息标题");
//        下面两个可以空
        huaWeiRequestVo.setGroupName("组名可以没有 填充字符类型");
        huaWeiRequestVo.setNotifySummary("通知栏名称");
        return huaWeiAppSendMsg.SendMSg(huaWeiRequestVo.getTokens(),huaWeiRequestVo.getTitle(),huaWeiRequestVo.getContent(),huaWeiRequestVo.getNotifySummary(),huaWeiRequestVo.getGroupName());
    }

    /**
     * 魅族的推送消息
     * @return
     */
    @RequestMapping("/meiZu")
    public String sendMeizu(){
//        模拟用户列表
        List<String> pushIds= new ArrayList<>();
        pushIds.add("7780918728129181");
        pushIds.add("2189787900978900");

//        参数类
        MeiZuRequestVo meiZuRequestVo =new MeiZuRequestVo();
        meiZuRequestVo.setTitle("消息标题");
        meiZuRequestVo.setContent("消息内容");
        meiZuRequestVo.setPushIds(pushIds);
      return meiZuAppSendMsg.sendMsg(meiZuRequestVo.getTitle(),meiZuRequestVo.getContent(),meiZuRequestVo.getPushIds());
    }

    /**
     * vivo的推送消息
     * @return
     */
    @RequestMapping("/vivo")
    public String getVivo(){
        //        模拟用户列表
        List<String> s =new ArrayList<>();
        s.add("12345678901234567890123 ");
        s.add("123456789012345678901234)");

//        vivo一次推送最大数目
        final int regidsSize =1000;
        VivoRequestVo vivoRequestVo =new VivoRequestVo();
        vivoRequestVo.setRegIds(s);
        vivoRequestVo.setRequestId("jhjsdhddkksss");
        vivoRequestVo.setToken(vivoAppSendMsg.getToken());
        vivoRequestVo.setTitle("消息表头");
        vivoRequestVo.setContent("消息内容");
        if (vivoRequestVo.getRegIds()!=null){
            if (vivoRequestVo.getRegIds().size()>regidsSize){
                return ResultJson.bad("推送数量大于1000,请修改",null);
            }
        }
        String token = vivoAppSendMsg.sendMsg(vivoRequestVo.getRegIds(),vivoRequestVo.getToken(),vivoRequestVo.getTitle(),vivoRequestVo.getContent(),vivoRequestVo.getRequestId());
        return token;
    }

    /**
     * 根据型号发送消息内容
     * @param phoneName 手机型号名称
     * @param jsonStr 对应型号所需的数据
     * @return
     */
    public String send(String phoneName,String jsonStr){
        if (!StringUtils.hasLength(jsonStr)){
            return ResultJson.bad("推送数据为空，失败！",null);
        }
        if (!StringUtils.hasLength(phoneName)){
            return ResultJson.bad("手机型号为空，失败！",null);
        }
//        手机型号判定
        if (phoneName.toLowerCase().contains("vivo")){
            VivoRequestVo vivoRequestVo =JSONObject.parseObject(jsonStr,VivoRequestVo.class);
            return vivoAppSendMsg.sendMsg(vivoRequestVo.getRegIds(),vivoRequestVo.getToken(),vivoRequestVo.getTitle(),vivoRequestVo.getContent(),vivoRequestVo.getRequestId());
        }else if (phoneName.toLowerCase().contains("meiZu")){
            MeiZuRequestVo meiZuRequestVo = JSONObject.parseObject(jsonStr,MeiZuRequestVo.class);
            return meiZuAppSendMsg.sendMsg(meiZuRequestVo.getTitle(),meiZuRequestVo.getContent(),meiZuRequestVo.getPushIds());
        }else if (phoneName.toLowerCase().contains("huaWei")){
            HuaWeiRequestVo huaWeiRequestVo = JSONObject.parseObject(jsonStr,HuaWeiRequestVo.class);
            return huaWeiAppSendMsg.SendMSg(huaWeiRequestVo.getTokens(),huaWeiRequestVo.getTitle(),huaWeiRequestVo.getContent(),huaWeiRequestVo.getNotifySummary(),huaWeiRequestVo.getGroupName());
        }

        return ResultJson.bad("不识别的手机型号",null);

    }

}
